﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default2 : System.Web.UI.Page
{
    MyDataDataContext db = new MyDataDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        BoundField field = new BoundField();
        field.HeaderText = "出售者";
        GridView1.Columns.Add(field); //增加列的另一种方式
        BoundField field1 = new BoundField();
        field1.HeaderText = "求购者";
        GridView2.Columns.Add(field1);
        
        foreach (GridViewRow gv in GridView1.Rows)//对应每一行记录，添加对应的出售者id
        {
            string saleID = gv.Cells[1].Text;//获取出售者id
            var saleName = (from r in db.User
                            where r.UserID == saleID
                            select r.UserNickname).FirstOrDefault();
            gv.Cells[6].Text = saleName;            
        }

        foreach (GridViewRow gv in GridView2.Rows)//对应每一行记录，添加对应的出售者id
        {
            string saleID = gv.Cells[1].Text;//获取出售者id
            var saleName = (from r in db.User
                            where r.UserID == saleID
                            select r.UserNickname).FirstOrDefault();
            gv.Cells[7].Text = saleName;
        }
    }
    //搜索
    protected void Search(object sender, EventArgs e)
    {
        string searchInput = Request["searchInput"];
        if(searchInput != null)
        {


        }

    }

    //出售物品收藏
    protected void GridView_OnRowCommang(object sender, GridViewCommandEventArgs e)
    {
        //收藏之前必须登录账号
        if (Session["UserId"] == null)//用户未登录，返回登录界面
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            if (e.CommandName != "Collect") return;
            string saleOfGoodsID = e.CommandArgument.ToString();

            var GoodsName = (from r in db.SaleOfGoods
                             where r.GoodsID == saleOfGoodsID
                             select r.GoodsName).FirstOrDefault();

            string str = System.Guid.NewGuid().ToString("N");
            string LsTime = DateTime.Now.ToString();//获取当下时间作为收藏时间
            Collection collect = new Collection
            {
                CollectionID = str,
                CollectionTime = LsTime,
                SaleOfGoodsID = saleOfGoodsID,
                UserID = Session["UserID"].ToString(),
                GoodsName = GoodsName
            };
            db.Collection.InsertOnSubmit(collect);
            //保存插入记录
            db.SubmitChanges();
            //弹出收藏创建成功提示
            Response.Write("<script language='javascript'>");
            Response.Write("alert('收藏物品成功，请前往首页-我的收藏查看。');");
            Response.Write("</script>");
        }
        
    }

    //出售物品
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {       
        //修改鼠标经过是行背景的颜色
        if (e.Row.RowType == DataControlRowType.DataRow)//判断当前的行是否属于datarow类型的行
        {
            //e.Row.Attributes.Add("ondblclick", "window.open('Detail.aspx?id=" + e.Row.Cells[2].Text + "')");//双击行打开新页
            e.Row.Attributes.Add("ondblclick", "window.open('Detail.aspx?GoodsID=" + e.Row.Cells[0].Text + "')");//双击行打开新页，并将GoodsId传递给物品详细页面
            //鼠标经过时
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#F0F8FF'");
            //鼠标移出时
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'"); 
        }
    }

    //出售物品，隐藏不显示的列;
    protected void ReportView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false;//物品id隐藏
            e.Row.Cells[1].Visible = false;//出售者id隐藏
        }
    }

    //求购物品收藏
    protected void GridView2_OnRowCommang(object sender, GridViewCommandEventArgs e)
    {
        //收藏之前必须登录账号
        if (Session["UserId"] == null)//用户未登录，返回登录界面
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            if (e.CommandName != "Collect") return;
            string lookOfGoodsID = e.CommandArgument.ToString();

            var GoodsName = (from r in db.LookingOfGoods
                             where r.GoodsID == lookOfGoodsID
                             select r.GoodsName).FirstOrDefault();

            string str = System.Guid.NewGuid().ToString("N");
            string LsTime = DateTime.Now.ToString();//获取当下时间作为收藏时间
            Collection collect = new Collection
            {
                CollectionID = str,
                CollectionTime = LsTime,
                SaleOfGoodsID = lookOfGoodsID,
                UserID = Session["UserID"].ToString(),
                GoodsName = GoodsName
            };
            db.Collection.InsertOnSubmit(collect);
            //保存插入记录
            db.SubmitChanges();
            //弹出收藏创建成功提示
            Response.Write("<script language='javascript'>");
            Response.Write("alert('收藏物品成功，请前往首页-我的收藏查看。');");
            Response.Write("</script>");
        }

    }

    //求购物品
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //修改鼠标经过是行背景的颜色
        if (e.Row.RowType == DataControlRowType.DataRow)//判断当前的行是否属于datarow类型的行
        {
            //e.Row.Attributes.Add("ondblclick", "window.open('Detail.aspx?id=" + e.Row.Cells[2].Text + "')");//双击行打开新页
            e.Row.Attributes.Add("ondblclick", "window.open('DetailOfLooking.aspx?GoodsID=" + e.Row.Cells[0].Text + " && t=1')");//双击行打开新页，并将GoodsId传递给物品详细页面
            //鼠标经过时
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#F0F8FF'");
            //鼠标移出时
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
    }

    //求购物品，隐藏不显示的列;
    protected void ReportView2_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false;//物品id隐藏
            e.Row.Cells[1].Visible = false;//求购者id隐藏
        }
    }
}